package com.baosight.sczx.common.zm.cInterface.cb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cb.domain.ZMCBNeighboringWorkingface;

/**
 * 采相邻采面对外接口
 * <p>Title: NeighboringWorkingface</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *        2013-7-16 上午11:01:49  高强 
 *</pre>
 */
public class NeighboringWorkingface {

	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	/**
	 * 插入相邻采面
	 * <p>Function: InsertNeighboringWorkingface</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *  2013-7-3 上午11:01:49  高强 
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean InsertNeighboringWorkingface(EiInfo inInfo,String MainId){ 
		
		try {
			String fdNeighborhoodId = inInfo.getBlock("result").getCell(0, "fdMainId").toString();
			if(!fdNeighborhoodId.equals(""))
			{
				String[] _temp = fdNeighborhoodId.split(";");
				for(int j = 0; j < _temp.length; j++)
				{
					String[] _tem = _temp[j].split(":");
					Map map = new HashMap();
					map.put("fdKeyId", TablePk.getInstance().getNextValue("T_CI_NEIGHBORING_WORKINGFACE"));//主键
					map.put("fdMainId", MainId);//主采面ID
					map.put("fdNeighborhoodId",  _tem[1]);//相邻采面主键
					map.put("fdPosition", _tem[0]);//方位
					dao.insert("ZMCBNeighboringWorkingface.insert", map);
					map = new HashMap();
					map.put("fdKeyId", TablePk.getInstance().getNextValue("T_CI_NEIGHBORING_WORKINGFACE"));//主键
					map.put("fdMainId",  _tem[1]);//主采面ID
					map.put("fdNeighborhoodId",MainId);//相邻采面主键
					map.put("fdPosition", getOppositePosition(_tem[0]));//方位
					dao.insert("ZMCBNeighboringWorkingface.insert", map);
				}
			}
		}catch (Exception e) {
			inInfo.setStatus(-1);
			inInfo.setMsg("插入相邻采面记录失败!");
			return false;
		}
		return true;
	}
	
	
	/**
	 * 删除相邻采面
	 * <p>Function: DeleteNeighboringWorkingface</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *  2013-7-3 上午11:01:49  高强 
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean DeleteNeighboringWorkingface(String MainId){ 
		
		try {
			
			Map map = new HashMap();
			map.put("fdMainId", MainId);//主采面ID
			dao.delete("ZMCBNeighboringWorkingface.deleteWf", map);
			
		}catch (Exception e) {
			return false;
		}
		return true;
	}
	
	/**
	 * 删除相邻采面
	 * <p>Function: DeleteNeighboringWorkingface</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *  2013-7-3 上午11:01:49  高强 
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean DeleteNeighboringOppositeWorkingface(String MainId){ 
		
		try {
			
			Map map = new HashMap();
			map.put("fdMainId", MainId);//主采面ID
			dao.delete("ZMCBNeighboringWorkingface.deleteWfOpposite", map);
			
		}catch (Exception e) {
			return false;
		}
		return true;
	}
	
	
	/**
	 * 更新相邻采面
	 * <p>Function: UpdateNeighboringWorkingface</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *  2013-7-3 上午11:01:49  高强 
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean UpdateNeighboringWorkingface(EiInfo inInfo,String MainId){ 
		
		try {
			String fdNeighborhoodId = inInfo.getBlock("result").getCell(0, "fdMainId").toString();
			if(!fdNeighborhoodId.equals(""))
			{
				if(!DeleteNeighboringWorkingface(MainId))
				{
					inInfo.setStatus(-1);
					inInfo.setMsg("删除相邻采面记录失败!");
					return false;
				}
				
				if(!DeleteNeighboringOppositeWorkingface(MainId))
				{
					inInfo.setStatus(-1);
					inInfo.setMsg("删除相邻相对采面记录失败!");
					return false;
				}
				String[] _temp = fdNeighborhoodId.split(";");
				for(int j = 0; j < _temp.length; j++)
				{
					String[] _tem = _temp[j].split(":");
					Map map = new HashMap();
					map.put("fdKeyId", TablePk.getInstance().getNextValue("T_CI_NEIGHBORING_WORKINGFACE"));//主键
					map.put("fdMainId", MainId);//主采面ID
					map.put("fdNeighborhoodId",  _tem[1]);//相邻采面主键
					map.put("fdPosition", _tem[0]);//方位
					dao.insert("ZMCBNeighboringWorkingface.insert", map);
					map = new HashMap();
					map.put("fdKeyId", TablePk.getInstance().getNextValue("T_CI_NEIGHBORING_WORKINGFACE"));//主键
					map.put("fdMainId",  _tem[1]);//主采面ID
					map.put("fdNeighborhoodId",MainId);//相邻采面主键
					map.put("fdPosition", getOppositePosition(_tem[0]));//方位
					dao.insert("ZMCBNeighboringWorkingface.insert", map);
				
				}
			}else
			{
				if(!DeleteNeighboringWorkingface(MainId))
				{
					inInfo.setStatus(-1);
					inInfo.setMsg("删除相邻采面记录失败!");
					return false;
				}
				
				if(!DeleteNeighboringOppositeWorkingface(MainId))
				{
					inInfo.setStatus(-1);
					inInfo.setMsg("删除相邻相对采面记录失败!");
					return false;
				}
			}
		}catch (Exception e) {
			inInfo.setStatus(-1);
			inInfo.setMsg("插入相邻采面记录失败!");
			return false;
		}
		return true;
	}
	
	
	public enum Position {  
	    东,西,南,北,上,下;

	    public static Position getPosition(String position){  
	       return valueOf(position.toLowerCase());  
	    }      
	}
	
	public static String getOppositePosition(String position)
	{
		String pos ="";
		switch(Position.getPosition(position)) {
		case 东:
			pos="西";
			return pos;
		case 西:
			pos= "东";
			return pos;
		case 南:
			pos= "北";
			return pos;
		case 北:
			pos= "南";
			return pos;
		case 上:
			pos= "下";
			return pos;
		case 下:
			pos= "上";
			return pos;
		}
		return pos;
	}
	
	/**
	 * 查询上方位的相邻采面
	 * <p>Function: getUpNeighboring</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-9-21上午10:16:42 张志芳
	 * </p>
	 *
	 */
	public static String getUpNeighboring(String workface){
		Map map = new HashMap();
		map.put("fdMainId", workface);
		map.put("fdPosition", "上");
		List<ZMCBNeighboringWorkingface> list = dao.query("ZMCBNeighboringWorkingface.query", map);
		if(list.size()>0){
			return list.get(0).getFdNeighborhoodId();
		}
		return null;
	}
	
	/**
	 * 查询下方位的相邻采面
	 * <p>Function: getDownNeighboring</p>
	 * <p>Description: </p>
	 * <p>return value:List<String></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-9-21下午01:55:46 张志芳
	 * </p>
	 *
	 */
	public static List<String> getDownNeighboring(String workface){
		List<String> list = new ArrayList<String>();
		Map map = new HashMap();
		map.put("fdMainId", workface);
		map.put("fdPosition", "下");
		List<ZMCBNeighboringWorkingface> tmp = dao.query("ZMCBNeighboringWorkingface.query", map);
		while(tmp.size()>0){
			list.add(tmp.get(0).getFdNeighborhoodId());
			map.remove("fdMainId");
			map.put("fdMainId", tmp.get(0).getFdNeighborhoodId());
			tmp = dao.query("ZMCBNeighboringWorkingface.query", map);
		}
		return list;
	}
}
